#include <qfile.h>

#include <mythtv/mythcontext.h>
#include <mythtv/mythdbcon.h>
#include <qregexp.h>

#include "songinfo.h"

bool operator==(const SongInfo& a, const SongInfo& b)
{
    if (a.getFilePath() == b.getFilePath())
        return true;
    return false;
}


void SongInfo::setField(QString field, QString data)
{
    if (field == "type")
        m_type = data;
    else if (field == "name")
        m_name = data;
    else if (field == "filepath")
        m_filepath = data;
    else
        cout << "Error: Invalid field " << field << endl;

}

QString SongInfo::getExtension()
{
    int pos = m_filepath.findRev(".");
    if (pos == -1)
        return NULL;

    pos = m_filepath.length() - pos;
    pos--;

    QString ext = m_filepath.right(pos);
    return ext;
}

void SongInfo::fillData()
{
    if (m_name == "")
    {
        return;
    }

    QString thequery = "SELECT name, songtitle, filepath, type"
        " FROM cdgmetadata WHERE name=\"" 
        + m_name + "\" and hostname=\"" 
        + gContext->GetHostName() + "\"";

    
    thequery += ";";

    MSqlQuery query(MSqlQuery::InitCon());
    query.exec(thequery);

    if (query.isActive() && query.size() > 0)
    {
        query.next();

        setName(query.value(0).toString());
        setTitle(query.value(1).toString());
        setFilePath(query.value(2).toString());
        setType(query.value(3).toString());
        setType(query.value(3).toString());
    }
}

QString SongInfo::getTitle()
{
    QString title;
    if(m_title.length()>0)
    {
        title = m_name + " - " + m_title;
    }
    else
    {
        title = m_filepath.right(m_filepath.length() -
                                m_filepath.findRev("/") - 1);
        if(title.length()==0)
           title = m_name;
        
        title.replace(QRegExp("_"), " ");
        title.replace(QRegExp("%20"), " ");
        title = title.left(title.findRev("."));
        title.replace(QRegExp("\\."), " ");
    }
//     title = eatBraces(title, "[", "]");
//     title = eatBraces(title, "(", ")");
//     title = eatBraces(title, "{", "}");

    return title.stripWhiteSpace();
}


void SongInfo::parseFilename(QString parsestr)
{
    QString artist = "([\\w\\s\\&\\'-\\\\.\\(\\)]+)";
    QString title  = "([\\w\\s\\&\\'-\\\\.\\(\\)]+)";
    QString text   = "[\\w\\s\\&\\'-\\\\.\\(\\)]+";
    QString numbers  = "[\\d]+";

    QString ltext  = "([\\w\\s\\&\\'-\\\\.]+)";
    QString skip  = ".+";
    

    QString str = "^"+parsestr+"$";     
    QString s = str;
    s.replace( QRegExp("%artist%"), artist );
    s.replace( QRegExp("%title%"), title );
    s.replace( QRegExp("%text%"), text );
    s.replace( QRegExp("%numbers%"), numbers );

    QString filename = getName();
    filename.replace(QRegExp(QString("_")), QString(" "));
    filename.simplifyWhiteSpace();
//    filename = QRegExp::escape(filename);

    QRegExp rx;
    rx.setMinimal(true);
    rx.setPattern(s);
//    cout << rx.errorString() << endl;

    cout << "--- START ---" << endl;    
 
    if(rx.search(filename)==0)
    {
        cout << "Found  " << rx.numCaptures () << " : " << rx.search(filename) << " in file: " << getName() << endl;
        cout << "cap1 = " << rx.cap(1).stripWhiteSpace() << endl;

        int artist = str.find(QRegExp("%artist%"));
        int title  = str.find(QRegExp("%title%"));
        if((artist>-1)&&(title>-1))
        {
            if(artist < title)
            {
                cout << "Artist: " << rx.cap(1).stripWhiteSpace() << endl;
                cout << "Title:  " << rx.cap(2).stripWhiteSpace() << endl;
                m_name = rx.cap(1).stripWhiteSpace();
                m_title = rx.cap(2).stripWhiteSpace();
            }
            else
            {
               cout << "Artist: " << rx.cap(2).stripWhiteSpace() << endl;
               cout << "Title:  " << rx.cap(1).stripWhiteSpace() << endl;
               m_name = rx.cap(2).stripWhiteSpace();
               m_title = rx.cap(1).stripWhiteSpace();
            }
        }
        else 
        {
            if(artist>-1)
               cout << "Only Artist: " << rx.cap(1).stripWhiteSpace() << " - SKIPPING" << endl;
            if(title>-1)
               cout << "Only Title:  " << rx.cap(1).stripWhiteSpace() << " - SKIPPING" << endl;
        }
    }
    else
    {
        cout << "NOT Found in file: " << getName() << " - SKIPPING" << endl;
    }
    cout << "--- END ---" << endl;    
}
